package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC787 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 不是因为复杂，而是因为经典
     * 未来需要再次复习此道题目
     * 挺经典的带限制条件的BFS，也可以用动态规划解决
     * @return:
     */
    class Solution {
        public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
            Queue<int[]> queue = new LinkedList<>();
            List<int[]>[] edge = new List[n];
            int[] prices = new int[n];

            for (int i = 0; i < n; ++i) {
                edge[i] = new ArrayList<>();
                prices[i] = Integer.MAX_VALUE;
            }

            for (int[] flight : flights) {
                edge[flight[0]].add(new int[]{flight[1], flight[2]});
            }

            prices[src] = 0;
            queue.add(new int[]{src, 0, prices[src]});

            while (!queue.isEmpty()) {
                int[] poll = queue.poll();
                if (poll[1] > k) break;// 这里是次数统计

                for (int[] next : edge[poll[0]]) {
                    if (prices[next[0]] > poll[2] + next[1]) {
                        prices[next[0]] = poll[2] + next[1];
                        queue.add(new int[]{next[0], poll[1] + 1, prices[next[0]]});
                    }
                }
            }
            return prices[dst] == Integer.MAX_VALUE ? -1 : prices[dst];
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC787 an = new LC787();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
